package top.lilele.adminSystem.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lilele.adminSystem.dto.UserMenuDto;
import top.lilele.adminSystem.entity.AdminResourceEntity;
import top.lilele.adminSystem.entity.AdminRoleEntity;
import top.lilele.adminSystem.entity.AdminRoleResourceEntity;
import top.lilele.adminSystem.mapper.AdminResourceMapper;
import top.lilele.adminSystem.mapper.AdminRoleMapper;
import top.lilele.adminSystem.mapper.AdminRoleResourceMapper;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lilele
 */
@Service
public class AdminResourceService {
    @Resource
    AdminResourceMapper adminResourceMapper;

    @Resource
    AdminRoleMapper adminRoleMapper;

    @Resource
    AdminRoleResourceMapper adminRoleResourceMapper;

    /**
     * 新增资源，并且绑定在超级管理员角色上
     *
     * @param adminResourceEntity
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void add(AdminResourceEntity adminResourceEntity) {
        adminResourceMapper.insert(adminResourceEntity);
        // 自动绑定系统管理员角色与该资源关联
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("isAdmin", 1);
        AdminRoleEntity adminRoleEntity = adminRoleMapper.selectOne(queryWrapper);
        String id = adminRoleEntity.getId();
        AdminRoleResourceEntity adminRoleResourceEntity = new AdminRoleResourceEntity();
        adminRoleResourceEntity.setRoleId(id);
        adminRoleResourceEntity.setResourceId(adminResourceEntity.getId());
        adminRoleResourceMapper.insert(adminRoleResourceEntity);
    }

    /**
     * 根据用户id查询用户角色-菜单信息
     *
     * @param id
     * @return
     */
    public List<UserMenuDto> selectMenuListByRoleAndUserId(String id) {
        List<UserMenuDto> list = adminResourceMapper.selectMenuListByRoleAndUserId(id, 0);
        List<UserMenuDto> finalList = list.stream().filter(item -> item.getParentId().equals("0")).map(item -> {
            item.setChildren(getChildMenu(item, list));
            return item;
        }).sorted(Comparator.comparing(UserMenuDto::getSort)).collect(Collectors.toList());
        return finalList;
    }

    /**
     * 查询用户的api列表，用于sa-token权限控制
     *
     * @param id
     * @return
     */
    public List<UserMenuDto> selectApiResourceByUserId(String id) {
        List<UserMenuDto> list = adminResourceMapper.selectMenuListByRoleAndUserId(id, 1);
        return list;
    }

    /**
     * 查询所有的资源
     *
     * @return
     */
    public List<AdminResourceEntity> selectAllResources() {
        List<AdminResourceEntity> list = adminResourceMapper.selectList(null);
        List<AdminResourceEntity> finalList = list.stream().filter(item -> item.getParentId().equals("0")).map(item -> {
            item.setChildren(getChild(item, list));
            return item;
        }).sorted(Comparator.comparing(AdminResourceEntity::getSort)).collect(Collectors.toList());
        return finalList;

    }

    public List<UserMenuDto> getChildMenu(UserMenuDto node, List<UserMenuDto> list) {
        return list.stream().filter(item -> item.getParentId().equals(node.getId())).map(item -> {
            item.setChildren(getChildMenu(item, list));
            return item;
        }).sorted(Comparator.comparing(UserMenuDto::getSort)).collect(Collectors.toList());
    }

    public List<AdminResourceEntity> getChild(AdminResourceEntity node, List<AdminResourceEntity> list) {
        return list.stream().filter(item -> item.getParentId().equals(node.getId())).map(item -> {
            item.setChildren(getChild(item, list));
            return item;
        }).sorted(Comparator.comparing(AdminResourceEntity::getSort)).collect(Collectors.toList());
    }

    public List<AdminRoleResourceEntity> queryResourceByRoleId(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("roleId", id);
        List<AdminRoleResourceEntity> list = adminRoleResourceMapper.selectList(queryWrapper);
        return list;
    }

    public AdminResourceEntity queryResourceById(String id) {
        return adminResourceMapper.selectById(id);
    }

    /**
     * 根据id禁用资源
     *
     * @param id
     */
    public void disableResourceById(String id) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("available", 2);
        updateWrapper.eq("id", id);
        updateWrapper.set("updatedTime", new Date());
        adminResourceMapper.update(null, updateWrapper);
    }

    /**
     * 根据id开启资源
     *
     * @param id
     */
    public void enableResourceById(String id) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("available", 1);
        updateWrapper.eq("id", id);
        updateWrapper.set("updatedTime", new Date());
        adminResourceMapper.update(null, updateWrapper);
    }

    public void updateResourceById(AdminResourceEntity adminResourceEntity) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", adminResourceEntity.getId());
        updateWrapper.set("label", adminResourceEntity.getLabel());
        updateWrapper.set("description", adminResourceEntity.getDescription());
        updateWrapper.set("ico", adminResourceEntity.getIco());
        adminResourceMapper.update(adminResourceEntity, updateWrapper);
    }
}
